package com.sharding.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharding.entity.Dict;
import com.sharding.entity.Tuser;
import com.sharding.entity.User;
import com.sharding.mapper.DictMapper;
import com.sharding.mapper.TuserMapper;
import com.sharding.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/sharding")
public class UserController {

    @Autowired
    private UserMapper userMapper;
    //------------测试水平分表-------------//
    @GetMapping("saveUser")
    public int saveUser(){
        User user = new User();
        Random random = new Random();
        int value = random.nextInt(20);
        user.setAge(value);
        long longValue = random.nextLong();
        user.setCid(longValue);
        user.setName("test"+value);
        userMapper.insert(user);
        //判断是在那个数据库user_1还是user_2
        int sign= (int) (user.getId()%2+1);
        return sign;
    }
    @GetMapping("find")
    public List<User>findUser(){
        List<User> users = userMapper.selectList(null);
        return users;
    }
    //------------测试水平分库-------------//
    @GetMapping("addUserDb")
    public String addUserDb(){
        User user = new User();
        Random random = new Random();
        int value = random.nextInt(20);
        user.setAge(value);
        long intValue = random.nextInt(10);
        user.setCid(intValue);
        user.setName("test"+value);
        userMapper.insert(user);
        //判断是在那个数据库test01还是test02
        int signDatabase= (int) (user.getId()%2+1);
        int signTables= (int) (intValue%2+1);
        return "添加的数据在数据库test0"+signDatabase+"中的表user_"+signTables;
    }
    @GetMapping("getUserById")
    public User getUserById(){
        //test01库 user_1表中,主要看控制台ShardingJDBC执行的SQL
        return userMapper.selectById(1483619764034097154L);
    }
    //------------测试垂直分库-------------//
    @Autowired
    private TuserMapper tuserMapper;
    @GetMapping("addTuser")
    public int addTuser(){
        Tuser tuser = new Tuser();
        Random random = new Random();
        int value = random.nextInt(20);
        String status=value+"";
        tuser.setUserStatus(status);
        tuser.setUserName("test"+value);
        return  tuserMapper.insert(tuser);
    }
    @GetMapping("findTuserById")
    public Tuser findTUserById(){
        QueryWrapper<Tuser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",690571154120769537L);
        return tuserMapper.selectOne(wrapper);
    }
    //------------测试公共表-------------//
    @Autowired
    private DictMapper dictMapper;
    @GetMapping("savaDict")
    public int saveDict(){
        Dict udict = new Dict();
        udict.setUserStatus("a");
        udict.setUserValue("已启用");
       return dictMapper.insert(udict);
    }
    @GetMapping("deleteDict")
    public void deleteDict(){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("t_id",690570461980917761L);
        dictMapper.delete(wrapper);

    }
}
